home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / public / ghostscript / src / gsdevice.c < prev    next >
C/C++ Source or Header  |  1994-08-01  |  19KB  |  655 lines

  1. /* Copyright (C) 1989, 1992, 1993 Aladdin Enterprises.  All rights reserved.
  2.  
  3. This file is part of Ghostscript.
  4.  
  5. Ghostscript is distributed in the hope that it will be useful, but
  6. WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility
  7. to anyone for the consequences of using it or for whether it serves any
  8. particular purpose or works at all, unless he says so in writing.  Refer
  9. to the Ghostscript General Public License for full details.
  10.  
  11. Everyone is granted permission to copy, modify and redistribute
  12. Ghostscript, but only under the conditions described in the Ghostscript
  13. General Public License.  A copy of this license is supposed to have been
  14. given to you along with Ghostscript so you can know your rights and
  15. responsibilities.  It should be in a file named COPYING.  Among other
  16. things, the copyright notice and this notice must be preserved on all
  17. copies.  */
  18.  
  19. /* gsdevice.c */
  20. /* Device operators for Ghostscript library */
  21. #include "math_.h"            /* for fabs */
  22. #include "memory_.h"            /* for memcpy */
  23. #include "gx.h"
  24. #include "gserrors.h"
  25. #include "gsprops.h"
  26. #include "gsutil.h"
  27. #include "gxarith.h"
  28. #include "gzstate.h"
  29. #include "gzdevice.h"
  30. #include "gxdevmem.h"
  31.  
  32. /* Import the device list from gdevs.c */
  33. extern gx_device *gx_device_list[];
  34.  
  35. /* Device definitions */
  36. /* Following defines the null device */
  37. private dev_proc_fill_rectangle(null_fill_rectangle);
  38. private dev_proc_copy_mono(null_copy_mono);
  39. private dev_proc_get_xfont_procs(null_get_xfont_procs);
  40. private dev_proc_get_xfont_device(null_get_xfont_device);
  41.  
  42. /* The null device procedure record is also used to fill in */
  43. /* NULL procedures in actual devices, so it must be complete. */
  44. private gx_device_procs null_procs = {
  45.     gx_default_open_device,
  46.     gx_default_get_initial_matrix,
  47.     gx_default_sync_output,
  48.     gx_default_output_page,
  49.     gx_default_close_device,
  50.     gx_default_map_rgb_color,
  51.     gx_default_map_color_rgb,
  52.     null_fill_rectangle,
  53.     gx_default_tile_rectangle,
  54.     null_copy_mono,
  55.     gx_default_copy_color,
  56.     gx_default_draw_line,
  57.     gx_default_get_bits,
  58.     gx_default_get_props,
  59.     gx_default_put_props,
  60.     gx_default_map_cmyk_color,
  61.     null_get_xfont_procs,
  62.     null_get_xfont_device
  63. };
  64. gx_device_null gs_null_device = {
  65.     sizeof(gx_device),
  66.     &null_procs,
  67.     "null",
  68.     0, 0,
  69.     72, 72,
  70.     no_margins,
  71.     dci_black_and_white,
  72.     1,
  73.     0                /* target */
  74. };
  75.  
  76. /* Fill in a single procedure */
  77. #define fill_default(procs, p, dproc)\
  78.   if ( (procs)->p == 0 ) (procs)->p = dproc
  79. #define fill_proc(procs, p)\
  80.   fill_default(procs, p, null_procs.p)
  81.  
  82. /* Fill in NULL procedures in a device procedure record. */
  83. void
  84. gx_device_procs_complete(register gx_device_procs *procs)
  85. {    fill_proc(procs, open_device);
  86.     fill_proc(procs, get_initial_matrix);
  87.     fill_proc(procs, sync_output);
  88.     fill_proc(procs, output_page);
  89.     fill_proc(procs, close_device);
  90.     fill_proc(procs, map_rgb_color);
  91.     fill_proc(procs, map_color_rgb);
  92.     /* NOT fill_rectangle */
  93.     fill_proc(procs, tile_rectangle);
  94.     /* NOT copy_mono */
  95.     fill_proc(procs, copy_color);        /* Bogus? */
  96.     fill_proc(procs, draw_line);
  97.     fill_proc(procs, get_bits);
  98.     fill_proc(procs, get_props);
  99.     fill_proc(procs, put_props);
  100.     fill_proc(procs, map_cmyk_color);
  101.     fill_default(procs, get_xfont_procs, gx_default_get_xfont_procs);
  102.     fill_default(procs, get_xfont_device, gx_default_get_xfont_device);
  103. }
  104. void
  105. gx_device_complete_procs(gx_device *dev)
  106. {    gx_device_procs_complete(dev->procs);
  107. }
  108.  
  109. /* Flush buffered output to the device */
  110. int
  111. gs_flushpage(gs_state *pgs)
  112. {    gx_device *dev = pgs->device->info;
  113.     return (*dev->procs->sync_output)(dev);
  114. }
  115.  
  116. /* Make the device output the accumulated page description */
  117. int
  118. gs_copypage(gs_state *pgs)
  119. {    return gs_output_page(pgs, 1, 0);
  120. }
  121. int
  122. gs_output_page(gs_state *pgs, int num_copies, int flush)
  123. {    gx_device *dev = pgs->device->info;
  124.     return (*dev->procs->output_page)(dev, num_copies, flush);
  125. }
  126.  
  127. /* Copy scan lines from an image device */
  128. int
  129. gs_copyscanlines(gx_device *dev, int start_y, byte *data, uint size,
  130.   int *plines_copied, uint *pbytes_copied)
  131. {    uint line_size = gx_device_raster(dev, 0);
  132.     uint count = size / line_size;
  133.     uint i;
  134.     byte *dest = data;
  135.     for ( i = 0; i < count; i++, dest += line_size )
  136.     {    int code = (*dev->procs->get_bits)(dev, start_y + i, dest, NULL);
  137.         if ( code < 0 )
  138.         {    /* Might just be an overrun. */
  139.             if ( start_y + i == dev->height ) break;
  140.             return_error(code);
  141.         }
  142.     }
  143.     if ( plines_copied != NULL )
  144.       *plines_copied = i;
  145.     if ( pbytes_copied != NULL )
  146.       *pbytes_copied = i * line_size;
  147.     return 0;
  148. }
  149.  
  150. /* Get the current device from the graphics state */
  151. gx_device *
  152. gs_currentdevice(const gs_state *pgs)
  153. {    return pgs->device->info;
  154. }
  155.  
  156. /* Get the name of a device */
  157. const char *
  158. gs_devicename(const gx_device *dev)
  159. {    return dev->dname;
  160. }
  161.  
  162. /* Get the initial matrix of a device. */
  163. void
  164. gs_deviceinitialmatrix(gx_device *dev, gs_matrix *pmat)
  165. {    fill_proc(dev->procs, get_initial_matrix);
  166.     (*dev->procs->get_initial_matrix)(dev, pmat);
  167. }
  168.  
  169. /* Get the N'th device from the known device list */
  170. gx_device *
  171. gs_getdevice(int index)
  172. {    int i;
  173.     for ( i = 0; gx_device_list[i] != 0; i++ )
  174.        {    if ( i == index ) return gx_device_list[i];
  175.        }
  176.     return 0;            /* index out of range */
  177. }
  178.  
  179. /* Clone an existing device. */
  180. int
  181. gs_copydevice(gx_device **pnew_dev, const gx_device *dev, const gs_memory_procs *mprocs)
  182. {    register gx_device *new_dev;
  183.     new_dev = (gx_device *)(*mprocs->alloc)(1, dev->params_size, "gs_copydevice");
  184.     if ( new_dev == 0 ) return_error(gs_error_VMerror);
  185.     memcpy(new_dev, dev, dev->params_size);
  186.     new_dev->is_open = 0;
  187.     *pnew_dev = new_dev;
  188.     return 0;
  189. }
  190.  
  191. /* Make a memory (image) device. */
  192. /* If num_colors = -16, -24, or -32, this is a true-color device; */
  193. /* otherwise, num_colors is the number of elements in the palette */
  194. /* (2^N or 3*2^N). */
  195. int
  196. gs_makeimagedevice(gx_device **pnew_dev, const gs_matrix *pmat,
  197.   uint width, uint height, const byte *colors, int num_colors,
  198.   const gs_memory_procs *mprocs)
  199. {    const gx_device_memory *old_dev;
  200.     register gx_device_memory *new_dev;
  201.     int palette_size = num_colors;
  202.     int bpp = 1;
  203.     int pcount;
  204.     int bits_per_pixel;
  205.     float x_pixels_per_unit, y_pixels_per_unit;
  206.     byte palette[256 * 3];
  207.     byte *dev_palette;
  208.     int has_color;
  209.     if ( width <= 0 || height <= 0 ) return_error(gs_error_rangecheck);
  210.     switch ( num_colors )
  211.        {
  212.     case 3*2:
  213.         palette_size = 2; bpp = 3;
  214.     case 2:
  215.         bits_per_pixel = 1; break;
  216.     case 3*4:
  217.         palette_size = 4; bpp = 3;
  218.     case 4:
  219.         bits_per_pixel = 2; break;
  220.     case 3*16:
  221.         palette_size = 16; bpp = 3;
  222.     case 16:
  223.         bits_per_pixel = 4; break;
  224.     case 3*256:
  225.         palette_size = 256; bpp = 3;
  226.     case 256:
  227.         bits_per_pixel = 8; break;
  228.     case -16:
  229.         bits_per_pixel = 16; palette_size = 0; break;
  230.     case -24:
  231.         bits_per_pixel = 24; palette_size = 0; break;
  232.     case -32:
  233.         bits_per_pixel = 32; palette_size = 0; break;
  234.     default:
  235.         return_error(gs_error_rangecheck);
  236.        }
  237.     old_dev = gdev_mem_device_for_bits(bits_per_pixel);
  238.     if ( old_dev == 0 )        /* no suitable device */
  239.         return_error(gs_error_rangecheck);
  240.     pcount = palette_size * 3;
  241.     /* Check to make sure the palette contains white and black, */
  242.     /* and, if it has any colors, the six primaries. */
  243.     if ( bits_per_pixel <= 8 )
  244.        {    const byte *p;
  245.         byte *q;
  246.         int primary_mask = 0;
  247.         int i;
  248.         has_color = 0;
  249.         for ( i = 0, p = colors, q = palette;
  250.               i < palette_size; i++, q += 3
  251.             )
  252.            {    int mask = 1;
  253.             switch ( bpp )
  254.                {
  255.             case 1:            /* gray */
  256.                 q[0] = q[1] = q[2] = *p++;
  257.                 break;
  258.             default:        /* bpp == 3, colored */
  259.                 q[0] = p[0], q[1] = p[1], q[2] = p[2];
  260.                 p += 3;
  261.                }
  262. #define shift_mask(b,n)\
  263.   switch ( b ) { case 0xff: mask <<= n; case 0: break; default: mask = 0; }
  264.             shift_mask(q[0], 4);
  265.             shift_mask(q[1], 2);
  266.             shift_mask(q[2], 1);
  267. #undef shift_mask
  268.             primary_mask |= mask;
  269.             if ( q[0] != q[1] || q[0] != q[2] )
  270.                 has_color = 1;
  271.            }
  272.         switch ( primary_mask )
  273.            {
  274.         case 129:        /* just black and white */
  275.             if ( has_color )    /* color but no primaries */
  276.                 return_error(gs_error_rangecheck);
  277.         case 255:        /* full color */
  278.             break;
  279.         default:
  280.             return_error(gs_error_rangecheck);
  281.            }
  282.        }
  283.     else
  284.         has_color = 1;
  285.     /*
  286.      * The initial transformation matrix must map 1 user unit to
  287.      * 1/72".  Let W and H be the width and height in pixels, and
  288.      * assume the initial matrix is of the form [A 0 0 B X Y].
  289.      * Then the size of the image in user units is (W/|A|,H/|B|),
  290.      * hence the size in inches is ((W/|A|)/72,(H/|B|)/72), so
  291.      * the number of pixels per inch is
  292.      * (W/((W/|A|)/72),H/((H/|B|)/72)), or (|A|*72,|B|*72).
  293.      * Similarly, if the initial matrix is [0 A B 0 X Y] for a 90
  294.      * or 270 degree rotation, the size of the image in user
  295.      * units is (W/|B|,H/|A|), so the pixels per inch are
  296.      * (|B|*72,|A|*72).  We forbid non-orthogonal transformation
  297.      * matrices.
  298.      */
  299.     if ( is_fzero2(pmat->xy, pmat->yx) )
  300.         x_pixels_per_unit = pmat->xx, y_pixels_per_unit = pmat->yy;
  301.     else if ( is_fzero2(pmat->xx, pmat->yy) )
  302.         x_pixels_per_unit = pmat->yx, y_pixels_per_unit = pmat->xy;
  303.     else
  304.         return_error(gs_error_undefinedresult);
  305.     /* All checks done, allocate the device. */
  306.     new_dev = (gx_device_memory *)(*mprocs->alloc)(1, old_dev->params_size, "gs_makeimagedevice(device)");
  307.     if ( new_dev == 0 ) return_error(gs_error_VMerror);
  308.     *new_dev = *old_dev;
  309.     new_dev->initial_matrix = *pmat;
  310.     new_dev->width = width;
  311.     new_dev->height = height;
  312.     new_dev->x_pixels_per_inch = fabs(x_pixels_per_unit) * 72;
  313.     new_dev->y_pixels_per_inch = fabs(y_pixels_per_unit) * 72;
  314.     if ( !has_color )
  315.         new_dev->color_info.max_rgb = 0,
  316.         new_dev->color_info.dither_rgb = 0;
  317.     dev_palette = (byte *)(*mprocs->alloc)(pcount, 1, "gs_makeimagedevice(palette)");
  318.     if ( dev_palette == 0 ) return_error(gs_error_VMerror);
  319.     new_dev->invert = (palette[0] | palette[1] | palette[2] ? -1 : 0);    /* bogus */
  320.     new_dev->palette_size = palette_size;
  321.     new_dev->palette = dev_palette;
  322.     memcpy(dev_palette, palette, pcount);
  323.     /* The bitmap will be allocated when the device is opened. */
  324.     new_dev->memory_procs = mprocs;
  325.     new_dev->is_open = 0;
  326.     *pnew_dev = (gx_device *)new_dev;
  327.     return 0;
  328. }
  329.  
  330. /* Set the device in the graphics state */
  331. int
  332. gs_setdevice(gs_state *pgs, gx_device *dev)
  333. {    register device *pdev = pgs->device;
  334.     int was_open = dev->is_open;
  335.     int code;
  336.     /* Initialize the device */
  337.     if ( !was_open )
  338.     {    gx_device_complete_procs(dev);
  339.         if ( gs_device_is_memory(dev) )
  340.         {    /* Set the target to the current device. */
  341.             gx_device *odev = pdev->info;
  342.             while ( odev != 0 && gs_device_is_memory(odev) )
  343.                 odev = ((gx_device_memory *)odev)->target;
  344.             ((gx_device_memory *)dev)->target = odev;
  345.         }
  346.         code = (*dev->procs->open_device)(dev);
  347.         if ( code < 0 ) return_error(code);
  348.         dev->is_open = 1;
  349.     }
  350.     /* Compute device white and black codes */
  351.     pdev->black = (*dev->procs->map_cmyk_color)(dev, 0, 0, 0, gx_max_color_value);
  352.     pdev->white = (*dev->procs->map_cmyk_color)(dev, 0, 0, 0, 0);
  353.     pdev->info = dev;
  354.     gx_set_cmap_procs(pgs);
  355.     if (    (code = gs_initmatrix(pgs)) < 0 ||
  356.         (code = gs_initclip(pgs)) < 0
  357.        )
  358.         return code;
  359.     if ( !was_open )
  360.         if ( (code = gs_erasepage(pgs)) < 0 )
  361.             return code;
  362.     return gx_remap_color(pgs);
  363. }
  364.  
  365. /* Select the null device.  This is just a convenience. */
  366. void
  367. gs_nulldevice(gs_state *pgs)
  368. {    gs_setdevice(pgs, (gx_device *)&gs_null_device);
  369. }
  370.  
  371. /* Close a device.  The client is responsible for ensuring that */
  372. /* this device is not current in any graphics state. */
  373. int
  374. gs_closedevice(gx_device *dev)
  375. {    int code = 0;
  376.     if ( dev->is_open )
  377.        {    code = (*dev->procs->close_device)(dev);
  378.         if ( code < 0 ) return_error(code);
  379.         dev->is_open = 0;
  380.        }
  381.     return code;
  382. }
  383.  
  384. /* Install enough of a null device to suppress graphics output */
  385. /* during the execution of stringwidth. */
  386. void
  387. gx_device_no_output(gs_state *pgs)
  388. {    pgs->device->info = (gx_device *)&gs_null_device;
  389. }
  390.  
  391. /* Just set the device without reinitializing. */
  392. /* (For internal use only.) */
  393. void
  394. gx_set_device_only(gs_state *pgs, gx_device *dev)
  395. {    pgs->device->info = dev;
  396. }
  397.  
  398. /* Compute the size of one scan line for a device, */
  399. /* with or without padding to a word boundary. */
  400. uint
  401. gx_device_raster(const gx_device *dev, int pad)
  402. {    ulong bits = (ulong)dev->width * dev->color_info.depth;
  403.     return (pad ?
  404.         (uint)((bits + (align_bitmap_mod * 8 - 1))
  405.              >> (log2_align_bitmap_mod + 3))
  406.             << log2_align_bitmap_mod :
  407.         (uint)((bits + 7) >> 3));
  408. }
  409.  
  410. /* Adjust the resolution for devices that only have a fixed set of */
  411. /* geometries, so that the apparent size in inches remains constant. */
  412. /* If fit=1, the resolution is adjusted so that the entire image fits; */
  413. /* if fit=0, one dimension fits, but the other one is clipped. */
  414. int
  415. gx_device_adjust_resolution(gx_device *dev,
  416.   int actual_width, int actual_height, int fit)
  417. {    double width_ratio = (double)actual_width / dev->width ;
  418.     double height_ratio = (double)actual_height / dev->height ;
  419.     double ratio =
  420.         (fit ? min(width_ratio, height_ratio) :
  421.          max(width_ratio, height_ratio));
  422.     dev->x_pixels_per_inch *= ratio;
  423.     dev->y_pixels_per_inch *= ratio;
  424.     dev->width = actual_width;
  425.     dev->height = actual_height;
  426.     return 0;
  427. }
  428.  
  429. /* ------ The null device ------ */
  430.  
  431. private int
  432. null_fill_rectangle(gx_device *dev, int x, int y, int w, int h,
  433.   gx_color_index color)
  434. {    return 0;
  435. }
  436. private int
  437. null_copy_mono(gx_device *dev, const byte *data,
  438.   int dx, int raster, gx_bitmap_id id, int x, int y, int w, int h,
  439.   gx_color_index zero, gx_color_index one)
  440. {    return 0;
  441. }
  442. private gx_xfont_procs *
  443. null_get_xfont_procs(gx_device *dev)
  444. {    gx_device *target = ((gx_device_null *)dev)->target;
  445.     return (target == 0 ? NULL :
  446.         (*target->procs->get_xfont_procs)(target));
  447. }
  448. private gx_device *
  449. null_get_xfont_device(gx_device *dev)
  450. {    gx_device *target = ((gx_device_null *)dev)->target;
  451.     return (target == 0 ? dev :
  452.         (*target->procs->get_xfont_device)(target));
  453. }
  454.  
  455. /* ------ Default device procedures ------ */
  456.  
  457. int
  458. gx_default_open_device(gx_device *dev)
  459. {    return 0;
  460. }
  461.  
  462. void
  463. gx_default_get_initial_matrix(register gx_device *dev, register gs_matrix *pmat)
  464. {    pmat->xx = dev->x_pixels_per_inch / 72.0;
  465.     pmat->xy = 0;
  466.     pmat->yx = 0;
  467.     pmat->yy = dev->y_pixels_per_inch / -72.0;
  468.     pmat->tx = 0;
  469.     pmat->ty = dev->height;    /****** WRONG for devices with ******/
  470.                 /****** arbitrary initial matrix ******/
  471. }
  472.  
  473. int
  474. gx_default_sync_output(gx_device *dev)
  475. {    return 0;
  476. }
  477.  
  478. int
  479. gx_default_output_page(gx_device *dev, int num_copies, int flush)
  480. {    return (*dev->procs->sync_output)(dev);
  481. }
  482.  
  483. int
  484. gx_default_close_device(gx_device *dev)
  485. {    return 0;
  486. }
  487.  
  488. int
  489. gx_default_copy_color(gx_device *dev, const byte *data,
  490.   int data_x, int raster, gx_bitmap_id id,
  491.   int x, int y, int width, int height)
  492. {    return (*dev->procs->copy_mono)(dev, data, data_x, raster, id,
  493.         x, y, width, height, (gx_color_index)0, (gx_color_index)1);
  494. }
  495.  
  496. int
  497. gx_default_get_bits(gx_device *dev, int y, byte *data, byte **actual_data)
  498. {    return -1;
  499. }
  500.  
  501. gx_xfont_procs *
  502. gx_default_get_xfont_procs(gx_device *dev)
  503. {    return NULL;
  504. }
  505.  
  506. gx_device *
  507. gx_default_get_xfont_device(gx_device *dev)
  508. {    return dev;
  509. }
  510.  
  511. /* Standard device properties */
  512.  
  513. private const gs_prop_item props_std[] = {
  514.         /* Following can be set, but will close and */
  515.         /* reopen the device if necessary. */
  516.     prop_def("HWResolution", prt_float_array),
  517.     prop_def("HWSize", prt_int_array),
  518.         /* Following cannot be set yet */
  519.     prop_def("InitialMatrix", prt_float_array),
  520.         /* Following cannot be set */
  521.     prop_def("Name", prt_string),
  522.         /* Slots for arrays */
  523.     prop_float, prop_float,
  524.     prop_int, prop_int,
  525.     prop_float, prop_float, prop_float, prop_float,
  526.       prop_float, prop_float
  527. };
  528.  
  529. /* Get the size of the device properties. */
  530. int
  531. gs_getdeviceprops_size(gx_device *dev)
  532. {    fill_proc(dev->procs, get_props);
  533.     return (*dev->procs->get_props)(dev, NULL);
  534. }
  535.  
  536. /* Get the device properties. */
  537. int
  538. gs_getdeviceprops(gx_device *dev, gs_prop_item *plist)
  539. {    fill_proc(dev->procs, get_props);
  540.     return (*dev->procs->get_props)(dev, plist);
  541. }
  542.  
  543. /* Get standard properties. */
  544. int
  545. gx_default_get_props(register gx_device *dev, register gs_prop_item *plist)
  546. {    if ( plist != 0 )
  547.        {    register gs_prop_item *pi;
  548.         gs_matrix mat;
  549.         memcpy(plist, props_std, sizeof(props_std));
  550.         plist[0].value.a.size = 2;
  551.         plist[1].value.a.size = 2;
  552.         plist[2].value.a.size = 6;
  553.         plist[3].value.a.p.s = (char *)dev->dname;
  554.         plist[3].value.a.size = -1;
  555.         pi = &plist[4];
  556.             /* resolution array */
  557.         plist[0].value.a.p.v = pi;
  558.         pi[0].value.f = dev->x_pixels_per_inch;
  559.         pi[1].value.f = dev->y_pixels_per_inch;
  560.         pi += 2;
  561.             /* width/height array */
  562.         plist[1].value.a.p.v = pi;
  563.         pi[0].value.i = dev->width;
  564.         pi[1].value.i = dev->height;
  565.         pi += 2;
  566.             /* matrix */
  567.         plist[2].value.a.p.v = pi;
  568.  
  569.         fill_proc(dev->procs, get_initial_matrix);
  570.         (*dev->procs->get_initial_matrix)(dev, &mat);
  571.         pi[0].value.f = mat.xx;
  572.         pi[1].value.f = mat.xy;
  573.         pi[2].value.f = mat.yx;
  574.         pi[3].value.f = mat.yy;
  575.         pi[4].value.f = mat.tx;
  576.         pi[5].value.f = mat.ty;
  577.         pi += 6;
  578.        }
  579.     return sizeof(props_std) / sizeof(gs_prop_item);
  580. }
  581.  
  582. /* Set the device properties. */
  583. /* If the device was open and the put_props procedure closed it, */
  584. /* return 1; otherwise, return 0 or an error code as usual. */
  585. int
  586. gs_putdeviceprops(gx_device *dev, gs_prop_item *plist, int count)
  587. {    int was_open = dev->is_open;
  588.     int code;
  589.     fill_proc(dev->procs, put_props);
  590.     code = (*dev->procs->put_props)(dev, plist, count);
  591.     return (code < 0 ? code : was_open && !dev->is_open ? 1 : code);
  592. }
  593.  
  594. /* Set standard properties. */
  595. /* Note that setting the size or resolution closes the device. */
  596. int
  597. gx_default_put_props(gx_device *dev, gs_prop_item *plist, int count)
  598. {    gs_prop_item *known[2];
  599.     int code = 0;
  600.     gx_device temp_dev;
  601.     props_extract(plist, count, props_std, 2, known, 1);
  602.     temp_dev = *dev;
  603.     if ( known[1] != 0 )
  604.        {    if ( known[1]->value.a.size != 2 )
  605.             known[1]->status = pv_typecheck,
  606.             code = gs_error_typecheck;
  607.         else
  608.            {    gs_prop_item *ap = known[1]->value.a.p.v;
  609.             if ( ap[0].value.i <= 0 || ap[1].value.i <= 0 )
  610.                 known[1]->status = pv_rangecheck,
  611.                 code = gs_error_rangecheck;
  612. #define max_coord min(max_int, fixed2long(max_fixed))
  613.             else if ( ap[0].value.i > max_coord ||
  614.                  ap[1].value.i > max_coord
  615.                )
  616.                 known[1]->status = pv_limitcheck,
  617.                 code = gs_error_limitcheck;
  618. #undef max_coord
  619.             else
  620.                {    temp_dev.width = ap[0].value.i;
  621.                 temp_dev.height = ap[1].value.i;
  622.                }
  623.             if ( code == 0 ) code = 1;
  624.            }
  625.        }
  626.     if ( known[0] != 0 )
  627.        {    if ( known[0]->value.a.size != 2 )
  628.             known[0]->status = pv_typecheck,
  629.             code = gs_error_typecheck;
  630.         else
  631.            {    gs_prop_item *ap = known[0]->value.a.p.v;
  632.             if ( ap[0].value.f <= 0 || ap[1].value.f <= 0 )
  633.                 known[0]->status = pv_rangecheck,
  634.                 code = gs_error_rangecheck;
  635.             else
  636.                {    temp_dev.x_pixels_per_inch = ap[0].value.f;
  637.                 temp_dev.y_pixels_per_inch = ap[1].value.f;
  638.                }
  639.             if ( code == 0 ) code = 1;
  640.            }
  641.        }
  642.     if ( code < 0 )
  643.         return_error(code);
  644.     /* Close the device; gs_putdeviceprops will reopen it. */
  645.     if ( dev->is_open && code )
  646.     {    int ccode = gs_closedevice(dev);
  647.         if ( ccode < 0 ) return ccode;
  648.     }
  649.     dev->x_pixels_per_inch = temp_dev.x_pixels_per_inch;
  650.     dev->y_pixels_per_inch = temp_dev.y_pixels_per_inch;
  651.     dev->width = temp_dev.width;
  652.     dev->height = temp_dev.height;
  653.     return code;
  654. }
  655.